package win.bigwhale.sudoku;

import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import win.bigwhale.sudoku.view.ChooseGame;

public class HomeScene extends Scene {

	private static BorderPane mPane = new BorderPane();
	private static HomeScene homeScene = new HomeScene();
	private Button startGameBt = new Button(SudokuUtil.START_GAME_BUTTON);
	private Button aboutGameBt = new Button(SudokuUtil.ABOUT_GAME_BUTTON);

	private HomeScene() {
		this(mPane, SudokuUtil.GAME_FIELD_WIDTH, SudokuUtil.GAME_FIELD_HEIGHT);
		VBox vBox = new VBox();
		vBox.getChildren().add(startGameBt);
		vBox.getChildren().add(aboutGameBt);
		vBox.setBackground(new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY)));
		vBox.setAlignment(Pos.CENTER);
		mPane.setCenter(vBox);
		mPane.setPadding(new Insets(15, 15, 15, 15));

		startGameBt.setPrefSize(100, 50);
		aboutGameBt.setPrefSize(100, 50);
		VBox.setMargin(startGameBt, new Insets(15.0));
		VBox.setMargin(aboutGameBt, new Insets(15.0));

		startGameBt.setOnMouseClicked(new StartGameEventHandler());
		aboutGameBt.setOnMouseClicked(new AboutGameEventHandler());
	}

	private HomeScene(Parent root, double width, double height) {
		super(root, width, height);
	}

	public static HomeScene getHomeScene() {
		return homeScene;
	}

	private final class AboutGameEventHandler implements EventHandler<MouseEvent> {
		@Override
		public void handle(MouseEvent event) {
			Alert aboutAlert = new Alert(AlertType.INFORMATION,
					null,
					new ButtonType("OK"));
			aboutAlert.setTitle("About this game");
			aboutAlert.setHeaderText("This game is developed by Liwei, all right reserved.");
			aboutAlert.setContentText("just a sudoku game demo.");
			aboutAlert.show();
		}
	}

	private final class StartGameEventHandler implements EventHandler<MouseEvent> {
		@Override
		public void handle(MouseEvent event) {
			ChooseGame.getChooseGame().show();
		}
	}
}
